#include "D3DUtil.h"
#include "FrameCalculator.h"
#include "tchar.h"
#include "cstdio"

FrameCalculator::FrameCalculator():mFPS(0.0f),mFont(0){
	D3DXFONT_DESC fontDesc;
	fontDesc.Height          = 18;
	fontDesc.Width           = 0;
	fontDesc.Weight          = 0;
	fontDesc.MipLevels       = 1;
	fontDesc.Italic          = false;
	fontDesc.CharSet         = DEFAULT_CHARSET;
	fontDesc.OutputPrecision = OUT_DEFAULT_PRECIS;
	fontDesc.Quality         = DEFAULT_QUALITY;
	fontDesc.PitchAndFamily  = DEFAULT_PITCH | FF_DONTCARE;
	strcpy_s(fontDesc.FaceName, _T("Times New Roman"));

	HR(D3DXCreateFontIndirect(gDevice, &fontDesc, &mFont));
}

FrameCalculator::~FrameCalculator(){
	ReleaseCOM(mFont);
}

void FrameCalculator::onLostDevice(){
	mFont->OnLostDevice();
}

void FrameCalculator::onResetDevice(){
	mFont->OnResetDevice();
}

void FrameCalculator::update(float dt){
	static float frame = 0.0f;
	static float passedTime = 0.0f;

	frame++;
	passedTime+=dt;

	if(passedTime>=1.0f){
		mFPS = frame;
		frame = 0;
		passedTime = 0;
	}
}

void FrameCalculator::draw(){
	static char buffer[256];
	sprintf_s(buffer, "FPS = %.2f",mFPS);
	RECT R = {5, 5, 0, 0};
	HR(mFont->DrawTextA(0,buffer,-1,&R,DT_NOCLIP,D3DCOLOR_XRGB(255,255,255)));
}
